﻿using System;
using System.Xml;
using Cyberwolf.Modules.PWCAPAdmin.Components;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Common;

using Cyberwolf.Modules.PWCAPAdmin.UserControls;

namespace Cyberwolf.Modules.PWCAPAdmin.Components
{
    public abstract class PWCAPAdminPageBaseClass : System.Web.UI.Page
    {
        #region Public Properties
        public string NoticeFlash
        {
            get
            {
                if (Session["PageNoticeFlash"] != null)
                {
                    return Session["PageNoticeFlash"].ToString();
                }
                return null;
            }
            set { Session["PageNoticeFlash"] = value; }
        }

        public string ErrorFlash
        {
            get
            {
                if (Session["PageErrorFlash"] != null)
                {
                    return Session["PageErrorFlash"].ToString();
                }
                return null;
            }
            set { Session["PageErrorFlash"] = value; }
        }

        public PWApplicationService AppService { get; private set; }
        #endregion

        #region Override Methods
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            try
            {
                var oModuleController = new ModuleController();
                var oModuleInfo = oModuleController.GetModule(int.Parse(Request["mid"]));
                this.AppService = new PWApplicationService((string)oModuleInfo.ModuleSettings["SERVICE_URL"]);
                if (PWSecurityToken.Current == null || !this.AppService.KeepAliveSession())
                {
                    ErrorFlash = "The session was expired. Please close the popup and login again.";
                }
            }
            catch(Exception ex)
            {
                ErrorFlash = ex.ToString();
            }
        }
        #endregion
    }
}
